package com.pearlsoft.rms.vcenter;

import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vmware.vim25.ManagedObjectReference;
import com.vmware.vim25.ServiceContent;
import com.vmware.vim25.VimPortType;
import com.vmware.vim25.VimService;

/**
 * 
 * @author rainshow
 * @Created 2012-5-17 下午05:03:18
 */

public class VcenterConnection {
    private static Log logger = LogFactory.getLog(VcenterConnection.class);
    private String url = null;
    private String userName = null;
    private String password = null;
    private VimService vimService = null;
    private VimPortType vimPort = null;
    private ServiceContent serviceContent = null;
    public static final String SVC_INST_NAME = "ServiceInstance";
    public static final ManagedObjectReference SVC_INST_REF = new ManagedObjectReference();
    protected ManagedObjectReference propCollector = null;
    protected ManagedObjectReference rootFolder = null;
    protected ManagedObjectReference perfManager;
    private boolean isConnected = false;

    public VcenterConnection(String url, String userName, String password) {
        this.url = url;
        this.userName = userName;
        this.password = password;
    }

    private void trustAllHttpsCertificates() throws Exception {
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
        javax.net.ssl.TrustManager tm = new TrustAllTrustManager();
        trustAllCerts[0] = tm;
        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
        javax.net.ssl.SSLSessionContext sslsc = sc.getServerSessionContext();
        sslsc.setSessionTimeout(30000);
        sc.init(null, trustAllCerts, null);
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    }

    public void connect() throws Exception {
        logger.info("开始登录VCENTER[" + url + "]...");
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        };
        trustAllHttpsCertificates();
        HttpsURLConnection.setDefaultHostnameVerifier(hv);

        SVC_INST_REF.setType(SVC_INST_NAME);
        SVC_INST_REF.setValue(SVC_INST_NAME);

        vimService = new VimService();
        vimPort = vimService.getVimPort();
        Map<String, Object> ctxt = ((BindingProvider) vimPort).getRequestContext();

        ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
        ctxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);

        serviceContent = vimPort.retrieveServiceContent(SVC_INST_REF);
        ((BindingProvider) vimPort).getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);
        vimPort.login(serviceContent.getSessionManager(), userName, password, null);
        logger.info("登录VCENTER成功!!!");
        isConnected = true;

        this.perfManager = serviceContent.getPerfManager();
        this.rootFolder = serviceContent.getRootFolder();
        this.propCollector = serviceContent.getPropertyCollector();
    }

    public void disconnect() throws Exception {
        if (isConnected) {
            vimPort.logout(serviceContent.getSessionManager());
        }
        isConnected = false;
    }

    public VimService getVimService() {
        return vimService;
    }

    public VimPortType getVimPort() {
        return vimPort;
    }

    public ServiceContent getServiceContent() {
        return serviceContent;
    }

    public ManagedObjectReference getPropCollector() {
        return propCollector;
    }

    public ManagedObjectReference getRootFolder() {
        return rootFolder;
    }

    public ManagedObjectReference getPerfManager() {
        return perfManager;
    }

}
